package com.mit.web.util
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.resources.ResourceManager;
	import mx.utils.ArrayUtil;
	
	public class LocaleUtil extends EventDispatcher
	{
		private static var me:LocaleUtil;
		public static const LANG_ZH_CN:String = "zh_CN";
		public static const LANG_ZH_TW:String = "zh_TW";
		public static const LANG_EN_US:String = "en_US";
		private var localeChain:Array = [LANG_EN_US, LANG_ZH_CN, LANG_ZH_TW];
		private var locale:String;

		
		public function LocaleUtil(target:IEventDispatcher=null)
		{
			super(target);
			if(me != null)
				throw new Error("Only one instance of this class is allowed, use getInstance() insteadof the constructor.");
		}
		
		public static function getInstance():LocaleUtil{
			if(me == null){
				me = new LocaleUtil();
			}
			return me;
		}
		
		public function changeLocale(lang:String):void{
			//Why need splice and unshift here? it is because that I want the ResourceManager 
			//continue to search the message in other language packages when it is not
			//defined in the specified resource.
			localeChain.splice(localeChain.indexOf(lang),1);
			localeChain.unshift(lang);
			
			ResourceManager.getInstance().localeChain = localeChain;
			locale = lang;
			dispatchEvent(new Event("Language change to " + lang));
		}
		
		public function get currentLocale():String{
			return locale;
		}
	}
}